# -*- coding: utf-8 -*-            
# @Author : chenliang
# @Time : 2024/1/18 14:06

from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.user_product_computing_locator import ProductComputingLocator
from Case.python_ui.Test_data.menu_name_data import MenuName
import random
import re
from Case.python_ui.PageLocate.product_management_locator import ProductManagementLocator
from Case.python_ui.Pageobject.admin_product_managent_pages import ProductManagementPages
from Case.python_ui.Common.business_common_tool import BusinessCommonTools

import time


class ProductComputing(Basepage):

    """公共方法"""
    # 等待页面加载转动
    def wait_web_page_rotate(self, time=100):
        doc = '等待页面加载转动图标'
        try:
            self.wait_elevisble(ProductComputingLocator.wait_for_pay, doc, time)
            self.wait_eleinvisble(ProductComputingLocator.wait_for_pay, doc, time)
        except:
            pass

    """
    
    """

    # 根据tab菜单名称进行点击
    def click_my_resource_tab(self, tab_name):
        doc = '根据my resource页的tab菜单名字进行点击进入'
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.my_resource_tab_list, doc)
        my_resource_tab_list = self.get_elements(ProductComputingLocator.my_resource_tab_list, doc)
        my_resource_tab_list_new = []
        for i in my_resource_tab_list:
            print('*****:', i.text, type(i))
            if i.text == tab_name:
                print('++++:', i.text)
                for a in range(3):
                    i.click()
                    if i.get_attribute('aria-selected') == 'true':
                        continue
                    else:
                        time.sleep(3)
                        break
            my_resource_tab_list_new.append(i.text)
        time.sleep(2)
        return my_resource_tab_list_new

    # 根据产品名称进行点击
    def get_product_name_to_click(self, product_name):
        doc = '根据产品名称然后点击'
        prod_list = self.get_elements(ProductComputingLocator.my_resource_product_list, doc)
        time.sleep(2)
        for prod in prod_list:
            print('+++++:', prod.text)
            # 20240318增加忽略大小写，不同环境菜单名字有大小写区别
            if prod.text.lower() == product_name.lower():
                prod.click()
                break
        time.sleep(5)

    # tab页根据产品名称进行点击进入控制台
    def click_product_by_name(self, product_name):
        doc = 'tab页根据产品名称进行点击进入控制台'
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.move_computing_create_new, doc)
        self.move_mouse_to_element(ProductComputingLocator.move_computing_create_new)
        time.sleep(2)
        self.get_product_name_to_click(product_name)
        time.sleep(2)

    """
        Auto Scaling
    """

    # 进入Auto Scaling管理控制页
    def enter_auto_scaling_page(self, cmdopt):
        doc = 'Auto Scaling管理控制页'
        time.sleep(5)
        self.click_my_resource_tab(MenuName.compute_name[cmdopt])
        self.click_product_by_name('Auto Scaling')
        for i in range(3):
            try:
                self.wait_elevisble(ProductComputingLocator.iframe_auto_scaling, doc)
                self.driver.switch_to.frame(self.get_element(ProductComputingLocator.iframe_auto_scaling, doc))
                self.wait_elevisble(ProductComputingLocator.if_enter_auto_scaling_page, doc)
                if self.get_element(ProductComputingLocator.if_enter_auto_scaling_page, doc).text == 'Auto Scaling':
                    break
            except:
                continue
        assert self.get_element(ProductComputingLocator.if_enter_auto_scaling_page, doc).text == 'Auto Scaling'
        self.wait_elevisble(ProductComputingLocator.click_scaling_groups_page, doc)
        self.click_element(ProductComputingLocator.click_scaling_groups_page, doc)
        time.sleep(15)
        self.wait_elevisble(ProductComputingLocator.if_enter_scaling_group, doc)
        text = '预期值: '+ 'Scaling Groups'+ '实际值: '+ self.get_element(ProductComputingLocator.if_enter_scaling_group, doc).text
        assert self.get_element(ProductComputingLocator.if_enter_scaling_group, doc).text == 'Scaling Groups',text
        time.sleep(5)

    # 创建一个Scaling Groups
    def create_scaing_groups(self):
        doc = '创建一个Scaling Groups'
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.click_create_scaling_group, doc)
        self.click_element(ProductComputingLocator.click_create_scaling_group, doc)
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.input_scaling_group_name, doc)
        random_number = random.randint(1, 100000)
        name = 'Scaling_Group_Name' + str(random_number)
        self.send_keys(ProductComputingLocator.input_scaling_group_name, name, doc)
        self.wait_elevisble(ProductComputingLocator.click_organization, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.click_organization, doc)
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.select_org_list, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.select_org_list, doc)
        time.sleep(5)
        # 判断vSwitch是否可用
        vpcid_list = []
        self.scroll_bar_operation(ProductComputingLocator.click_vpcid)
        self.wait_elevisble(ProductComputingLocator.click_vpcid, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.click_vpcid, doc)
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.vpcid_list, doc)
        vpcid_list = self.get_elements(ProductComputingLocator.vpcid_list, doc)
        print('vpclist长度:', len(vpcid_list))
        self.move_mouse_to_click_element(ProductComputingLocator.vpcid_list_num(self, '1'), doc)
        for i in range(len(vpcid_list)):
            try:
                self.wait_elevisble(ProductComputingLocator.if_vswitch, doc, 10)
                self.get_element(ProductComputingLocator.if_vswitch, doc).text
            except:
                break
            str1 = ProductComputingLocator.vpcid_list[1]
            print('ad:', str1)
            num = i + 1
            print('选择的路径:', num)
            self.wait_elevisble(ProductComputingLocator.click_vpcid, doc)
            self.move_mouse_to_click_element(ProductComputingLocator.click_vpcid, doc)
            time.sleep(2)
            self.wait_elevisble(ProductComputingLocator.vpcid_list_num(num), doc)
            self.move_mouse_to_click_element(ProductComputingLocator.vpcid_list_num(num), doc)
            time.sleep(5)

        self.wait_elevisble(ProductComputingLocator.click_ok, doc)
        self.click_element(ProductComputingLocator.click_ok, doc)
        # 提交后判断create提示成功
        self.wait_elevisble(ProductComputingLocator.show_create, doc)
        self.wait_eleinvisble(ProductComputingLocator.show_create, doc)
        print('创建的Scaling Groups Name ：', name)
        return name

    # 根据 scaling group name查询
    def search_scaling_group_name(self, group_name):
        doc = '根据scaling group name进行查询'
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.input_group_name, doc)
        self.send_keys(ProductComputingLocator.input_group_name, group_name, doc)
        self.wait_elevisble(ProductComputingLocator.click_search_button, doc)
        self.click_element(ProductComputingLocator.click_search_button, doc)
        time.sleep(5)
        # 交验
        self.wait_elevisble(ProductComputingLocator.total_data, doc)
        self.save_screen(doc)
        assert self.get_element(ProductComputingLocator.total_data, doc).text == 'Total 1'
        self.wait_elevisble(ProductComputingLocator.first_data_group_name, doc)
        assert self.get_element(ProductComputingLocator.first_data_group_name, doc).text == group_name

    # 删除列表第一条数据
    def delete_scaling_group_name(self):
        doc = '删除列表第一条scaling group数据'
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.total_data, doc)
        total_text = self.get_element(ProductComputingLocator.total_data, doc).text
        total_old = re.findall('\d', total_text)
        self.wait_elevisble(ProductComputingLocator.first_data_delete, doc)
        self.click_element(ProductComputingLocator.first_data_delete, doc)
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.click_sure_ok, doc)
        self.click_element(ProductComputingLocator.click_sure_ok, doc)
        time.sleep(5)
        # 判断删除是否成功
        # 不动查询条件，点击再次查询，看total是否减少1
        self.wait_elevisble(ProductComputingLocator.click_search_button, doc)
        self.click_element(ProductComputingLocator.click_search_button, doc)
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.total_data, doc)
        total_text = self.get_element(ProductComputingLocator.total_data, doc).text
        total_new = re.findall('\d', total_text)
        print('旧数据:', total_old[0], type(total_old[0]), '新数据:', total_new[0], type(total_new[0]))
        assert int(total_old[0]) - int(total_new[0]) == 1

    # 进入Scaling Group Details
    def enter_scaling_group_details(self, group_name):
        doc = '进入Scaling Group Details详细页面'
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.first_data_group_name, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.first_data_group_name, doc)
        time.sleep(5)
        # 获取basic information页面的scaling_group_name用作判断
        self.wait_elevisble(ProductComputingLocator.find_scaling_group_name, doc)
        find_scaling_group_name = self.get_element(ProductComputingLocator.find_scaling_group_name, doc).text
        assert find_scaling_group_name == group_name

    # 从Scaling Group Details详细页面退出到Scaling Groups页面
    def goback_from_Details_to_scaling_groups(self):
        doc = '从Scaling Group Details详细页面退出到Scaling Groups页面'
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.go_back, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.go_back, doc)
        time.sleep(2)

    """
       VM
    """

    # 进入VM管理控制页
    def enter_vm_page(self, cmdopt):
        doc = 'VM 管理控制页'
        self.wait_web_page_rotate(10)
        self.click_my_resource_tab(MenuName.compute_name[cmdopt])
        self.click_product_by_name('Vmware Virtual Machine')
        time.sleep(5)
        for i in range(3):
            try:
                self.switch_to_new_window()
                self.wait_elevisble(ProductComputingLocator.check_vm_title, doc)
                if self.get_element(ProductComputingLocator.check_vm_title, doc).text == 'Virtual Machine List':
                    break
            except:
                continue
        assert self.get_element(ProductComputingLocator.check_vm_title, doc).text == 'Virtual Machine List'

    # 根据network name进行查询
    def search_network_name(self, network_name):
        doc = '根据network name查询'
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.search_input_network_name, doc)
        self.send_keys(ProductComputingLocator.search_input_network_name, network_name, doc)
        self.wait_elevisble(ProductComputingLocator.click_search_network, doc)
        self.click_element(ProductComputingLocator.click_search_network, doc)
        time.sleep(2)
        # 交验
        self.wait_elevisble(ProductComputingLocator.network_total_data, doc)
        assert self.get_element(ProductComputingLocator.network_total_data, doc).text == 'Total 1 items'
        self.wait_elevisble(ProductComputingLocator.network_first_data_name, doc)
        assert self.get_element(ProductComputingLocator.network_first_data_name, doc).text == network_name

    # 创建network
    def enter_vm_page_add_network(self):
        doc = '创建network'
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.chick_network_page, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.chick_network_page, doc)
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.add_network, doc)
        self.click_element(ProductComputingLocator.add_network, doc)
        # 进入Create Network 页面，开始输入值
        random_number = random.randint(1, 100000)
        network_name = 'Network' + str(random_number)
        self.wait_elevisble(ProductComputingLocator.input_network_name, doc)
        self.send_keys(ProductComputingLocator.input_network_name, network_name, doc)
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.click_network_submit, doc)
        self.click_element(ProductComputingLocator.click_network_submit, doc)
        self.wait_elevisble(ProductComputingLocator.check_add_network_create, doc)
        self.wait_eleinvisble(ProductComputingLocator.check_add_network_create, doc)
        return network_name

    # 删除network
    def enter_vm_page_delete_network(self):
        doc = '删除network'
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.chick_network_page, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.chick_network_page, doc)
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.delete_network_list, doc)
        delete_network_list = self.get_elements(ProductComputingLocator.delete_network_list, doc)
        for i in delete_network_list:
            i.click()
            self.wait_elevisble(ProductComputingLocator.cilck_network_ok, doc)
            self.click_element(ProductComputingLocator.cilck_network_ok, doc)
            self.wait_elevisble(ProductComputingLocator.sure_delete_create, doc)
            self.wait_eleinvisble(ProductComputingLocator.sure_delete_create, doc)
        time.sleep(2)

    def get_vcenter_num(self, cmdopt, get_database_source):
        doc = '获取当前用户所在地区'
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.get_now_vcenter, doc)
        get_now_vcenter = self.get_element(ProductComputingLocator.get_now_vcenter, doc).text
        network_create_num = BusinessCommonTools().get_network_num(cmdopt, get_database_source, get_now_vcenter)
        return network_create_num

    def add_network_constraint(self, cmdopt, get_database_source):
        doc = '连续创建network'
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.chick_network_page, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.chick_network_page, doc)
        time.sleep(2)
        try:
            self.wait_elevisble(ProductComputingLocator.delete_network_list, doc)
            num = self.get_elements(ProductComputingLocator.delete_network_list, doc)
        except:
            num = []
        network_create_num = self.get_vcenter_num(cmdopt, get_database_source)
        a = 0
        for i in range(network_create_num + 1 - len(num)):
            self.wait_elevisble(ProductComputingLocator.add_network, doc)
            self.click_element(ProductComputingLocator.add_network, doc)
            # 进入Create Network 页面，开始输入值
            random_number = random.randint(1, 100000)
            network_name = 'Network' + str(random_number)
            self.wait_elevisble(ProductComputingLocator.input_network_name, doc)
            self.send_keys(ProductComputingLocator.input_network_name, network_name, doc)
            time.sleep(2)
            self.wait_elevisble(ProductComputingLocator.click_network_submit, doc)
            self.click_element(ProductComputingLocator.click_network_submit, doc)
            if a != network_create_num - len(num):
                self.wait_elevisble(ProductComputingLocator.check_add_network_create, doc)
                self.wait_eleinvisble(ProductComputingLocator.check_add_network_create, doc)
            else:
                self.wait_elevisble(ProductComputingLocator.create_on_line_cue, doc)
                self.wait_eleinvisble(ProductComputingLocator.create_on_line_cue, doc)
            a = a + 1

    # 判断进入 Create Virtual Machine创建页面
    # TL707905 | VM-create入口
    def enter_vm_create_page(self):
        doc = '判断进入Create Virtual Machine页面'
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.create_virtual_machine, doc)
        self.click_element(ProductComputingLocator.create_virtual_machine, doc)
        time.sleep(5)
        # 目前有两个页面，进入第三个页面
        # self.switch_to_new_window()
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[2])
        self.wait_elevisble(ProductComputingLocator.sure_into_vmware_virtual_machine, doc)
        assert self.get_element(ProductComputingLocator.sure_into_vmware_virtual_machine,
                                doc).text.lower() == 'Vmware Virtual Machine'.lower()

    # 检查vm 估算值是否正确
    def check_create_vm_estimated_costs(self, cmdopt):
        doc = 'vm创建页面检查估算值'
        self.enter_vm_create_page()
        self.wait_elevisble(ProductComputingLocator.first_instance_pricing, doc)
        instance_pricing = self.get_element(ProductComputingLocator.first_instance_pricing, doc).text
        # 使用正则表达式模式对字符串进行拆分
        pattern = r"[ /]"  # 模式表示空格出现一次或多次
        instance_pricing_result = re.split(pattern, instance_pricing)
        self.wait_elevisble(ProductComputingLocator.add_disk_list, doc)
        capacity_result = self.get_attribute(ProductComputingLocator.add_disk_list, 'value', doc)
        print('instance_pricing_result:', float(instance_pricing_result[1]))
        print('capacity_result:', int(capacity_result))
        ## 计算总值
        instance_type = float(instance_pricing_result[1])
        print('******:',MenuName.sum_of_money_mb[cmdopt])
        capacity = int(capacity_result) * 1024 * MenuName.sum_of_money_mb[cmdopt]
        capacity = round(capacity, 2)
        print('capacity:', capacity)
        tax = (instance_type + capacity) * MenuName.tax[cmdopt]
        tax = round(tax, 2)
        print('tax:', tax)
        total = instance_type + capacity + tax
        total = round(total, 2)
        print('total:',total)
        try:
            self.wait_elevisble(ProductComputingLocator.calculating_price,doc)
            self.wait_eleinvisble(ProductComputingLocator.calculating_price,doc)
        except:
            pass
        self.wait_elevisble(ProductComputingLocator.costs,doc)
        costs=self.get_element(ProductComputingLocator.costs,doc).text
        costs_result = re.split(pattern, costs)
        print('costs_result:',float(costs_result[0].replace(',','')))
        text = '实际值:'+ str(total) + '系统值:'+ str(float(costs_result[0].replace(',','')))
        assert float(costs_result[0].replace(',','')) == total,text
        self.driver.close()
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[1])
        return instance_type,capacity

    # 编辑datastore查看估算值是否正确
    def check_modify_vm_estimated_costs(self):
        doc = '配置vm的modify_datastore'
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.click_search,doc)
        self.click_element(ProductComputingLocator.click_search,doc)
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.virtual_machine_list_first_data_name, doc)
        first_data_name = self.get_element(ProductComputingLocator.virtual_machine_list_first_data_name, doc).text
        self.wait_elevisble(ProductComputingLocator.rig_virtual_machine_list_first, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.rig_virtual_machine_list_first, doc)
        self.wait_elevisble(ProductComputingLocator.virtual_machine_list_first_data_modify_datastore, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.virtual_machine_list_first_data_modify_datastore, doc)
        time.sleep(5)
        # 目前有两个页面，进入第三个页面
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[2])
        self.wait_elevisble(ProductComputingLocator.titel_change_datastore, doc)
        self.save_screen(doc)
        assert self.get_element(ProductComputingLocator.titel_change_datastore, doc).text == 'Change Datastore'
        time.sleep(5)
        # 添加 disk
        self.wait_elevisble(ProductComputingLocator.click_add_disk, doc)
        self.click_element(ProductComputingLocator.click_add_disk, doc)
        self.wait_elevisble(ProductComputingLocator.input_disk_capacity, doc)
        time.sleep(5)
        # 使用正则表达式模式对字符串进行拆分
        pattern = r"[ /]"  # 模式表示空格出现一次或多次
        self.wait_elevisble(ProductComputingLocator.costs,doc)
        costs=self.get_element(ProductComputingLocator.costs,doc).text
        costs_result = re.split(pattern, costs)
        return float(costs_result[0].replace(',', ''))




    # 查询判断vm状态
    def search_by_name_virtual_machine(self, search_term, virtual_machine_name, instance_state='Powered off'):
        doc = '根据virtual_machine_name搜索进行查询'
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.click_reset, doc)
        self.click_element(ProductComputingLocator.click_reset, doc)
        self.wait_elevisble(ProductComputingLocator.click_search_virtual_machine_list, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.click_search_virtual_machine_list, doc)
        time.sleep(2)
        search_virtual_machine_terms = self.get_elements(ProductComputingLocator.search_virtual_machine_term, doc)
        for i in search_virtual_machine_terms:
            if i.text == search_term:
                i.click()
                break
        self.wait_elevisble(ProductComputingLocator.input_search_term, doc)
        self.send_keys(ProductComputingLocator.input_search_term, virtual_machine_name, doc)
        self.wait_elevisble(ProductComputingLocator.click_search, doc)
        self.click_element(ProductComputingLocator.click_search, doc)
        time.sleep(2)
        if search_term == 'Name':
            assert self.get_element(ProductComputingLocator.virtual_machine_list_first_data_name,
                                    doc).text == virtual_machine_name
        if search_term == 'Instance ID':
            assert self.get_element(ProductComputingLocator.virtual_machine_list_first_data_id,
                                    doc).text == virtual_machine_name
        assert self.get_element(ProductComputingLocator.virtual_machine_list_first_data_state,
                                doc).text == instance_state
        return self.get_element(ProductComputingLocator.virtual_machine_list_first_data_id, doc).text

    # 创建 Create Virtual Machine
    # #TL707926 | VM购买流程-支付成功
    def create_buy_virtual_machine(self):
        doc = '创建并bug virtual_machine'
        self.enter_vm_create_page()
        random_number = random.randint(1, 100000)
        virtual_machine_name = 'virtual_machine' + str(random_number)
        self.wait_elevisble(ProductComputingLocator.input_vmware_virtual_machine_name, doc)
        self.send_keys(ProductComputingLocator.input_vmware_virtual_machine_name, virtual_machine_name, doc)
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.click_buy_now, doc)
        self.click_element(ProductComputingLocator.click_buy_now, doc)
        time.sleep(5)
        #self.scroll_bar_operation(ProductComputingLocator.tick_terms_of_service)
        self.wait_elevisble(ProductComputingLocator.tick_terms_of_service, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.tick_terms_of_service, doc)
        self.wait_elevisble(ProductComputingLocator.click_payment_pay, doc)
        self.click_element(ProductComputingLocator.click_payment_pay, doc)
        try:
            self.wait_elevisble(ProductComputingLocator.wait_for_submit, doc)
            self.wait_eleinvisble(ProductComputingLocator.wait_for_submit, doc)
        except:
            pass
        # 判断是否创建进入创建成功页面
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.payment_successful, doc)
        assert self.get_element(ProductComputingLocator.payment_successful, doc).text == 'Payment Successful'
        self.driver.close()
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[1])
        return virtual_machine_name

    # #TL707923 | payment页面查看 检查
    def create_buy_virtual_machine_into_Payment(self):
        doc = '进入payment页面查看检查'
        self.enter_vm_create_page()
        random_number = random.randint(1, 100000)
        virtual_machine_name = 'virtual_machine' + str(random_number)
        self.wait_elevisble(ProductComputingLocator.input_vmware_virtual_machine_name, doc)
        self.send_keys(ProductComputingLocator.input_vmware_virtual_machine_name, virtual_machine_name, doc)
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.click_buy_now, doc)
        self.click_element(ProductComputingLocator.click_buy_now, doc)
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.sure_enter_into_payment, doc)
        assert self.get_element(ProductComputingLocator.sure_enter_into_payment, doc).text == 'Payment'
        self.wait_elevisble(ProductComputingLocator.move_to_detail, doc)
        self.move_mouse_to_element(ProductComputingLocator.move_to_detail)
        self.wait_elevisble(ProductComputingLocator.detail_instance_name, doc)
        assert self.get_element(ProductComputingLocator.detail_instance_name, doc).text == virtual_machine_name

    # 删除Create Virtual Machine
    def release_virtual_machine(self):
        doc = 'VM控制台 release_virtual_machine'
        time.sleep(5)

        self.wait_elevisble(ProductComputingLocator.rig_virtual_machine_list, doc)
        release_virtual_machine_list = self.get_elements(ProductComputingLocator.rig_virtual_machine_list, doc)
        for i in release_virtual_machine_list:
            self.wait_elevisble(ProductComputingLocator.rig_virtual_machine_list_first, doc)
            self.move_mouse_to_click_element(ProductComputingLocator.rig_virtual_machine_list_first, doc)

            self.wait_elevisble(ProductComputingLocator.click_virtual_machine_release, doc)
            self.click_element(ProductComputingLocator.click_virtual_machine_release, doc)
            time.sleep(5)
            # 目前有两个页面，进入第三个页面
            # self.switch_to_new_window()
            all_handles = self.driver.window_handles
            self.driver.switch_to.window(all_handles[2])
            self.wait_elevisble(ProductComputingLocator.sure_virtual_machine_release_submit, doc)
            self.click_element(ProductComputingLocator.sure_virtual_machine_release_submit, doc)
            time.sleep(2)
            self.wait_elevisble(ProductComputingLocator.sure_virtual_machine_yes_release, doc)
            self.click_element(ProductComputingLocator.sure_virtual_machine_yes_release, doc)
            time.sleep(5)
            self.wait_elevisble(ProductComputingLocator.sure_virtual_machine_release_success, doc)
            assert self.get_element(ProductComputingLocator.sure_virtual_machine_release_success,
                                    doc).text == 'Release Successfully'
            # release成功回到第二个页面
            self.driver.close()
            all_handles = self.driver.window_handles
            self.driver.switch_to.window(all_handles[1])
            time.sleep(2)
            self.wait_elevisble(ProductComputingLocator.click_reset, doc)
            self.click_element(ProductComputingLocator.click_reset, doc)
            time.sleep(5)

        time.sleep(2)

    # 查看VM实例详情页
    def check_virtual_machine_detail_page(self):
        doc = '检查virtual_machine详情页'
        vm_data_item_list = {}
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.virtual_machine_list_first_data_name, doc)
        first_data_name = self.get_element(ProductComputingLocator.virtual_machine_list_first_data_name, doc).text
        first_data_id = self.get_element(ProductComputingLocator.virtual_machine_list_first_data_id, doc).text
        first_data_state = self.get_element(ProductComputingLocator.virtual_machine_list_first_data_state, doc).text
        first_data_host = self.get_element(ProductComputingLocator.virtual_machine_list_first_data_host, doc).text
        first_data_cpu = self.get_element(ProductComputingLocator.virtual_machine_list_first_data_cpu, doc).text
        first_data_memory = self.get_element(ProductComputingLocator.virtual_machine_list_first_data_memory, doc).text
        vm_data_item_list['first_data_name'] = first_data_name
        vm_data_item_list['first_data_id'] = first_data_id
        vm_data_item_list['first_data_state'] = first_data_state
        vm_data_item_list['first_data_host'] = first_data_host
        vm_data_item_list['first_data_cpu'] = first_data_cpu
        vm_data_item_list['first_data_memory'] = first_data_memory
        # 进入vm详情页面
        vm_basic_information_data_item1 = ['Instance Name', 'Instance ID', 'Instance State', 'Operating System', 'CPU',
                                           'Memory', 'Instance Type', 'IP', 'Create Time', 'Cluster', 'Datastore']
        vm_basic_information_data_item2 = {}
        self.move_mouse_to_click_element(ProductComputingLocator.virtual_machine_list_first_data_id, doc)

        time.sleep(5)
        # 判断页面数据项是否丢失
        self.wait_elevisble(ProductComputingLocator.basic_information_list, doc)
        basic_information_list = self.get_elements(ProductComputingLocator.basic_information_list, doc)
        r = 0
        for i in basic_information_list:
            print(i.text)
            print(vm_basic_information_data_item1[r])
            assert i.text == vm_basic_information_data_item1[r].strip()
            r = r + 1
        # 获取数据想对应的值
        self.wait_elevisble(ProductComputingLocator.basic_information_data_list, doc)
        basic_information_data_list = self.get_elements(ProductComputingLocator.basic_information_data_list, doc)
        i = 0
        for r in basic_information_data_list:
            vm_basic_information_data_item2[vm_basic_information_data_item1[i]] = r.text
            i = i + 1
        print('basic_information_data_list:', basic_information_data_list)
        # 校验数据项
        assert vm_basic_information_data_item2['Instance Name'] == vm_data_item_list['first_data_name']
        assert vm_basic_information_data_item2['Instance ID'] == vm_data_item_list['first_data_id']
        assert vm_basic_information_data_item2['Instance State'] == vm_data_item_list['first_data_state']

    # switch on the power按钮
    def action_switch_on_power(self, powered='true'):
        doc = '更改实例状态为Powered on 或Powered off'
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.virtual_machine_list_first_data_name, doc)
        first_data_name = self.get_element(ProductComputingLocator.virtual_machine_list_first_data_name, doc).text
        self.wait_elevisble(ProductComputingLocator.rig_virtual_machine_list_first, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.rig_virtual_machine_list_first, doc)
        if powered == 'true':
            self.wait_elevisble(ProductComputingLocator.virtual_machine_list_first_data_on_power, doc)
            self.move_mouse_to_click_element(ProductComputingLocator.virtual_machine_list_first_data_on_power, doc)
        else:
            self.wait_elevisble(ProductComputingLocator.virtual_machine_list_first_data_off_power, doc)
            self.move_mouse_to_click_element(ProductComputingLocator.virtual_machine_list_first_data_off_power, doc)
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.click_power_ok, doc)
        self.click_element(ProductComputingLocator.click_power_ok, doc)
        try:
            self.wait_elevisble(ProductComputingLocator.wait_for_submit, doc)
            self.wait_eleinvisble(ProductComputingLocator.wait_for_submit, doc)
        except:
            pass
        time.sleep(2)
        if powered == 'true':
            self.search_by_name_virtual_machine('Name', first_data_name, 'Powered on')
        else:
            self.search_by_name_virtual_machine('Name', first_data_name, 'Powered off')

    # #TL707887 | action-suspend
    def action_suspend(self):
        doc = '更改vm状态为suspend'
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.virtual_machine_list_first_data_name, doc)
        first_data_name = self.get_element(ProductComputingLocator.virtual_machine_list_first_data_name, doc).text
        self.wait_elevisble(ProductComputingLocator.rig_virtual_machine_list_first, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.rig_virtual_machine_list_first, doc)
        self.wait_elevisble(ProductComputingLocator.virtual_machine_list_first_data_suspend, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.virtual_machine_list_first_data_suspend, doc)
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.click_power_ok, doc)
        self.click_element(ProductComputingLocator.click_power_ok, doc)
        try:
            self.wait_elevisble(ProductComputingLocator.wait_for_submit, doc)
            self.wait_eleinvisble(ProductComputingLocator.wait_for_submit, doc)
        except:
            pass
        # 等待状态变更
        time.sleep(5)
        for i in range(3):
            try:
                self.search_by_name_virtual_machine('Name', first_data_name, 'Suspended')
                break
            except:
                time.sleep(20)
        self.search_by_name_virtual_machine('Name', first_data_name, 'Suspended')

    # #TL707887 | action-suspend
    def action_reboot(self):
        doc = '更改vm状态为reboot'
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.virtual_machine_list_first_data_name, doc)
        first_data_name = self.get_element(ProductComputingLocator.virtual_machine_list_first_data_name, doc).text
        self.wait_elevisble(ProductComputingLocator.rig_virtual_machine_list_first, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.rig_virtual_machine_list_first, doc)
        self.wait_elevisble(ProductComputingLocator.virtual_machine_list_first_data_reboot, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.virtual_machine_list_first_data_reboot, doc)
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.click_power_ok, doc)
        self.click_element(ProductComputingLocator.click_power_ok, doc)
        try:
            self.wait_elevisble(ProductComputingLocator.wait_for_submit, doc)
            self.wait_eleinvisble(ProductComputingLocator.wait_for_submit, doc)
        except:
            pass
        # 等待状态变更
        time.sleep(5)
        for i in range(3):
            try:
                self.search_by_name_virtual_machine('Name', first_data_name, 'Powered on')
                break
            except:
                time.sleep(20)
        self.search_by_name_virtual_machine('Name', first_data_name, 'Powered on')

    # TL707899 | action-modify datastore
    def modify_datastore(self):
        doc = '配置vm的modify_datastore'
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.virtual_machine_list_first_data_name, doc)
        first_data_name = self.get_element(ProductComputingLocator.virtual_machine_list_first_data_name, doc).text
        self.wait_elevisble(ProductComputingLocator.rig_virtual_machine_list_first, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.rig_virtual_machine_list_first, doc)
        self.wait_elevisble(ProductComputingLocator.virtual_machine_list_first_data_modify_datastore, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.virtual_machine_list_first_data_modify_datastore, doc)
        time.sleep(5)
        # 目前有两个页面，进入第三个页面
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[2])
        self.wait_web_page_rotate(10)
        self.wait_elevisble(ProductComputingLocator.titel_change_datastore, doc)
        self.save_screen(doc)
        assert self.get_element(ProductComputingLocator.titel_change_datastore, doc).text == 'Change Datastore'
        # 添加 disk
        self.wait_elevisble(ProductComputingLocator.click_add_disk, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.click_add_disk, doc)
        self.wait_elevisble(ProductComputingLocator.input_disk_capacity, doc)
        # input_element=self.get_element(ProductComputingLocator.input_disk_capacity, doc)
        # #input_element = self.driver.find_element_by_xpath('(//*[@aria-valuemin="1"])[2]')
        # self.excute_js2("arguments[0].value = '1';", input_element)
        # #self.send_keys(ProductComputingLocator.input_disk_capacity,'1',doc)

        self.wait_elevisble(ProductComputingLocator.click_buy_now, doc)
        self.click_element(ProductComputingLocator.click_buy_now, doc)
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.tick_terms_of_service, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.tick_terms_of_service, doc)
        self.wait_elevisble(ProductComputingLocator.click_payment_pay, doc)
        self.click_element(ProductComputingLocator.click_payment_pay, doc)
        try:
            self.wait_elevisble(ProductComputingLocator.wait_for_submit, doc)
            self.wait_eleinvisble(ProductComputingLocator.wait_for_submit, doc)
        except:
            pass
        # 判断是否创建进入创建成功页面
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.payment_successful, doc)
        assert self.get_element(ProductComputingLocator.payment_successful, doc).text == 'Payment Successful'
        self.driver.close()
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[1])

    def copy_instance_id(self):
        doc = 'instance_id单机复制功能'
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.virtual_machine_list_first_data_copy, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.virtual_machine_list_first_data_copy, doc)
        self.wait_elevisble(ProductComputingLocator.search_input_network_name, doc)
        self.keyboard_event_copy(ProductComputingLocator.search_input_network_name, doc)
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.click_search_network, doc)
        self.click_element(ProductComputingLocator.click_search_network, doc)
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.list_count, doc)
        assert self.get_element(ProductComputingLocator.list_count, doc).text == '1'

        # 判断进入 Create Virtual Machine创建页面
        # TL707905 | VM-create入口

    # enter_vm_create_page判断Datastore数据项
    def enter_vm_create_page_check_datastore(self):
        doc = '进入Create Virtual Machine页面,检查Datastore数据项'
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.create_virtual_machine, doc)
        self.click_element(ProductComputingLocator.create_virtual_machine, doc)
        time.sleep(5)
        # 目前有两个页面，进入第三个页面
        # self.switch_to_new_window()
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[2])
        try:
            self.wait_elevisble(ProductComputingLocator.wait_for_submit, doc)
            self.wait_eleinvisble(ProductComputingLocator.wait_for_submit, doc)
        except:
            pass
        self.wait_elevisble(ProductComputingLocator.sure_into_vmware_virtual_machine, doc)
        assert self.get_element(ProductComputingLocator.sure_into_vmware_virtual_machine,
                                doc).text.lower() == 'Vmware Virtual Machine'.lower()
        self.wait_elevisble(ProductComputingLocator.get_datastore_data(self, '2'), doc)
        assert self.get_element(ProductComputingLocator.get_datastore_data(self, '2'), doc).text == 'Datastore Name'
        self.wait_elevisble(ProductComputingLocator.get_datastore_data(self, '3'), doc)
        assert self.get_element(ProductComputingLocator.get_datastore_data(self, '3'), doc).text == 'Datastore Type'
        self.wait_elevisble(ProductComputingLocator.get_datastore_data(self, '4'), doc)
        assert self.get_element(ProductComputingLocator.get_datastore_data(self, '4'),
                                doc).text == 'Available/Total Storage'
        self.wait_elevisble(ProductComputingLocator.get_datastore_data(self, '5'), doc)
        assert self.get_element(ProductComputingLocator.get_datastore_data(self, '5'), doc).text == 'Status'

    # create VM页面参数编辑--Capacity
    def enter_vm_create_page_check_capacity(self):
        doc = 'create VM页面参数编辑--Capacity'
        doc = '进入Create Virtual Machine页面,检查Datastore数据项'
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.create_virtual_machine, doc)
        self.click_element(ProductComputingLocator.create_virtual_machine, doc)
        time.sleep(5)
        # 目前有两个页面，进入第三个页面
        # self.switch_to_new_window()
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[2])
        try:
            self.wait_elevisble(ProductComputingLocator.wait_for_submit, doc, 20)
            self.wait_eleinvisble(ProductComputingLocator.wait_for_submit, doc, 20)
        except:
            pass
        self.wait_elevisble(ProductComputingLocator.sure_into_vmware_virtual_machine, doc)
        assert self.get_element(ProductComputingLocator.sure_into_vmware_virtual_machine,
                                doc).text.lower() == 'Vmware Virtual Machine'.lower()
        # print(self.is_element_disabled(ProductComputingLocator.click_add_disk))
        # 添加空间直到占满
        while True:
            self.wait_elevisble(ProductComputingLocator.click_add_disk, doc)
            print(self.is_element_disabled(ProductComputingLocator.click_add_disk))
            if self.is_element_disabled(ProductComputingLocator.click_add_disk) == True:
                self.click_element(ProductComputingLocator.click_add_disk, doc)
                continue
            else:
                break
        self.wait_elevisble(ProductComputingLocator.capacity_full_clue, doc)
        self.wait_elevisble(ProductComputingLocator.add_disk_list, doc)
        add_disk_list = self.get_elements(ProductComputingLocator.add_disk_list, doc)
        total = 0
        for i in add_disk_list:
            value = i.get_attribute("value")
            total = total + int(value)
        print('value:', total)
        self.wait_elevisble(ProductComputingLocator.datastore_available_total_storage, doc)
        datastore_available_total_storage = self.get_element(ProductComputingLocator.datastore_available_total_storage,
                                                             doc).text
        print(self.get_element(ProductComputingLocator.datastore_available_total_storage, doc).text)
        # 使用正则表达式模式对字符串进行拆分
        pattern = r"[%/() ]"  # 模式表示逗号、感叹号、问号或空格出现一次或多次
        result = re.split(pattern, datastore_available_total_storage)
        assert int(result[0]) + total == int(result[1])
        # print(result)

    # 检查cpu
    def enter_vm_create_page_check_cpu(self):
        doc = 'create VM页面检查cpu'
        doc = '进入Create Virtual Machine页面,检查Datastore数据项'
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.create_virtual_machine, doc)
        self.click_element(ProductComputingLocator.create_virtual_machine, doc)
        time.sleep(5)
        # 目前有两个页面，进入第三个页面
        # self.switch_to_new_window()
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[2])
        try:
            self.wait_elevisble(ProductComputingLocator.wait_for_submit, doc, 20)
            self.wait_eleinvisble(ProductComputingLocator.wait_for_submit, doc, 20)
        except:
            pass
        self.wait_elevisble(ProductComputingLocator.sure_into_vmware_virtual_machine, doc)
        assert self.get_element(ProductComputingLocator.sure_into_vmware_virtual_machine,
                                doc).text.lower() == 'Vmware Virtual Machine'.lower()
        self.wait_elevisble(ProductComputingLocator.click_instance_type_cpu, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.click_instance_type_cpu, doc)
        self.wait_elevisble(ProductComputingLocator.instance_type_cpu_list, doc)
        instance_type_cpu_list = self.get_elements(ProductComputingLocator.instance_type_cpu_list, doc)
        new_instance_type_cpu_list = []
        for i in instance_type_cpu_list:
            new_instance_type_cpu_list.append(i.text)
            if i.text == '1 Core':
                i.click()
        assert new_instance_type_cpu_list[0] == '1 Core'
        assert new_instance_type_cpu_list[1] == '2 Core'
        assert new_instance_type_cpu_list[2] == '4 Core'
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.check_instance_type_cpu_list, doc)
        check_instance_type_cpu_list = self.get_elements(ProductComputingLocator.check_instance_type_cpu_list, doc)
        for i in check_instance_type_cpu_list:
            assert i.text == '1'

    # 检查memory
    def enter_vm_create_page_check_memory(self):
        doc = 'create VM页面检查cpu'
        doc = '进入Create Virtual Machine页面,检查Datastore数据项'
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.create_virtual_machine, doc)
        self.click_element(ProductComputingLocator.create_virtual_machine, doc)
        time.sleep(5)
        # 目前有两个页面，进入第三个页面
        # self.switch_to_new_window()
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[2])
        try:
            self.wait_elevisble(ProductComputingLocator.wait_for_submit, doc, 20)
            self.wait_eleinvisble(ProductComputingLocator.wait_for_submit, doc, 20)
        except:
            pass
        self.wait_elevisble(ProductComputingLocator.sure_into_vmware_virtual_machine, doc)
        assert self.get_element(ProductComputingLocator.sure_into_vmware_virtual_machine,
                                doc).text.lower() == 'Vmware Virtual Machine'.lower()
        self.wait_elevisble(ProductComputingLocator.click_instance_type_memory, doc)
        self.move_mouse_to_click_element(ProductComputingLocator.click_instance_type_memory, doc)
        self.wait_elevisble(ProductComputingLocator.instance_type_cpu_memory, doc)
        instance_type_memory_list = self.get_elements(ProductComputingLocator.instance_type_cpu_memory, doc)
        new_instance_type_memory_list = []
        for i in instance_type_memory_list:
            new_instance_type_memory_list.append(i.text)
            if i.text == '1 GiB':
                i.click()
        assert new_instance_type_memory_list[0] == '1 GiB'
        assert new_instance_type_memory_list[1] == '2 GiB'
        assert new_instance_type_memory_list[2] == '4 GiB'
        assert new_instance_type_memory_list[3] == '8 GiB'
        assert new_instance_type_memory_list[4] == '16 GiB'
        time.sleep(2)
        self.wait_elevisble(ProductComputingLocator.check_instance_type_memory_list, doc)
        check_instance_type_memory_list = self.get_elements(ProductComputingLocator.check_instance_type_memory_list,
                                                            doc)
        for i in check_instance_type_memory_list:
            assert i.text == '1'
